<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>房间信息列表</title>
		<base href="<%=request.getContextPath()%>/">
		<link rel="stylesheet" type="text/css" href="css/room.css"/>
		<script src="js/public/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/private/room.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div class="room-list">
			<table border="" cellspacing="" cellpadding="" width="100%" id="tab">
				<tr>
					<th>房间地址</th>
					<th>可容纳人数</th>
					<th>已住人数</th>
					<th>房租</th>
					<th>房间状态</th>
					<th>房东</th>
					<th>房东电话</th>
					<th>房间类型</th>
					<th></th>
				</tr>
				<!--  <tr align="center">
					<td id="address">新南路10号</td>
					<td id="capacity">10</td>
					<td id="livenumber">6</td>
					<td id="rent">1600元/月</td>
					<td id="state">正常</td>
					<td id="lname">李四</td>
					<td id="tel">18889099097</td>
					<td id="type">男生宿舍</td>
					<td><input type="radio" name="select-room" id="select-room" value="" /></td>
					
				</tr>-->
			</table>
		</div>
		<div class='page' id="pages">
		
			<div class='page-item first' id='first'></div>
		
			<div class='page-item prev' id='prev'></div>
			
			<div class='page-item' id='mid'></div>
			<div class='page-item next' id='next'></div>
			<div class='page-item last' id='last'></div>
		</div>
		<div class="op">
		<shiro:hasAnyRoles name="admin,landlord">
			<button type="button" id="addroom">添加房间</button>
			<button type="button" class="del" id="delRoom">删除房间</button><br><br>
			</shiro:hasAnyRoles>
			<button type="button" id="findroominfo">查看房间信息</button>
			<shiro:lacksRole name="teacher">
			<button type="button" class="find" id="selectm">查看房间设施</button>
			</shiro:lacksRole>
		</div>
		<div class="find-room">
			<table border="1px" cellspacing="0px" cellpadding="0px" width="100%" height="100%">
			<tr>
		        <th colspan="4">房间信息查询</th>
			 </tr>
				<tr>
					<td>房间地址</td>
					<td><input type="text" value="" id="address"/></td>
					<td>房间状态</td>
					<td><select name="state" id="state">
					   <option value="0">不限</option>
						<option value="1">正常</option>
						<option value="2">设施损坏</option>
					</select></td>
				</tr>
				<tr>
					<td>可住房间</td>
					<td><select name="access-room" id="enableLiving">
						<option value="0">不限</option>
						<option value="1">可住房间</option>
					</select></td>
					<td>房间类型</td>
					<td><select name="room-type" id="type">
						<option value="0">不限</option>
						<option value="1">男生宿舍</option>
						<option value="2">女生宿舍</option>
					</select></td>
				</tr>
				<tr>
					<td colspan="4"><input type="button" name="" id="find" value="查询" /></td>
					
				</tr>
			</table>
		</div>
	</body>
	<script type="text/javascript">
	//查询所有的房间信息
	$(function(){

		var curPage=${param.curPage};
		$.ajax({
			url:"room/findRoomsByCurpage",
			type:"get",
			data:{"curPage":curPage},
			success:function(data){
				//<tr align="center">
				//<td id="address">新南路10号</td>
				//<td id="capacity">10</td>
				//<td id="livenumber">6</td>
				//<td id="rent">1600元/月</td>
				//<td id="state">正常</td>
				//<td id="lname">李四</td>
				//<td id="tel">18889099097</td>
				//<td id="type">男生宿舍</td>
				//<td><input type="radio" name="select-room" id="select-room" value="" /></td>
			//</tr>
			$.each(data.data,function(index,val){
			var tr=$("<tr align='center'></tr>");
			var td1=$("<td></td>");
			td1.text(val.address);
			var td2=$("<td></td>");
				td2.text(val.capacity);
				var td3=$("<td></td>");
				td3.text(val.livenumber);
				var td4=$("<td></td>");
				td4.text(val.rent);
				var td5=$("<td></td>");
				if(val.state==1){
				td5.text("正常");
				}else if(val.state==2){
					td5.text("设施损坏");
				}
				var td6=$("<td></td>");
				td6.text(val.landlord.lname);
				var td7=$("<td></td>");
				td7.text(val.landlord.tel);
				var td8=$("<td></td>");
				if(val.type==1)
				td8.text("男生宿舍");
				else if(val.type==2)
					td8.text("女生宿舍");
				var td9=$("<td></td>");
				var input=$("<input type='radio' name='select-room'/>");
				input.val(val.id);
				td9.append(input);
				tr.append(td1);
				tr.append(td2);
				tr.append(td3);
				tr.append(td4);
				tr.append(td5);
				tr.append(td6);
				tr.append(td7);
				tr.append(td8);
				tr.append(td9);
				$("#tab").append(tr);
			});
			if(data.currentPage==1&&data.currentPage!=data.totalSize){
			$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");

			$("#mid").html(data.currentPage+"/"+data.totalSize);
			$("#next").html("<a href="+data.url+"?curPage="+data.nextPage+">下一页</a>");
			$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
			} if(data.currentPage==data.totalSize&&data.currentPage!=1){
				$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
				$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
				$("#prev").html("<a href="+data.url+"?curPage="+data.prevPage+">上一页</a>");
				$("#mid").html(data.currentPage+"/"+data.totalSize);
			}if(data.currentPage!=1&&data.currentPage!=data.totalSize){
			$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
			$("#prev").html("<a href="+data.url+"?curPage="+data.prevPage+">上一页</a>");
			$("#mid").html(data.currentPage+"/"+data.totalSize);
			$("#next").html("<a href="+data.url+"?curPage="+data.nextPage+">下一页</a>");
			$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
			}
			if(data.currentPage==1&&data.currentPage==data.totalSize){
				$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
				$("#mid").html(data.currentPage+"/"+data.totalSize);
				$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
			}
		}
	});
		//根据条件筛选出条件匹配的房间信息
		$("#find").click(function(){
			var address=$("#address").val();
			var state=$("#state option:selected").val();
			var enableLiving=$("#enableLiving option:selected").val();
			var type=$("#type option:selected").val();
			$.ajax({
				url:"room/findRoomsByLimits",
				data:{'address':address,'state':state,'enableLiving':enableLiving,'type':type,'curPage':1},
				type:"post",
				success:function(data){
					if(data.data.length==0){
						alert("无房间信息，请重新输入查询条件查询");
						return;
					}
					$.each(data.data,function(index,val){
						var tr=$("<tr align='center'></tr>");
						var td1=$("<td></td>");
						td1.text(val.address);
						var td2=$("<td></td>");
							td2.text(val.capacity);
							var td3=$("<td></td>");
							td3.text(val.livenumber);
							var td4=$("<td></td>");
							td4.text(val.rent);
							var td5=$("<td></td>");
							if(val.state==1){
							td5.text("正常");
							}else if(val.state==2){
								td5.text("设施损坏");
							}
							var td6=$("<td></td>");
							td6.text(val.landlord.lname);
							var td7=$("<td></td>");
							td7.text(val.landlord.tel);
							var td8=$("<td></td>");
							if(val.type==1)
							td8.text("男生宿舍");
							else if(val.type==2)
								td8.text("女生宿舍");
							var td9=$("<td></td>");
							var input=$("<input type='radio' name='select-room'/>");
							input.val(val.id);
							td9.append(input);
							tr.append(td1);
							tr.append(td2);
							tr.append(td3);
							tr.append(td4);
							tr.append(td5);
							tr.append(td6);
							tr.append(td7);
							tr.append(td8);
							tr.append(td9);
							if(index==0){
								$("#tab").html("<tr><th>房间地址</th><th>可容纳人数</th><th>已住人数</th><th>房租</th><th>房间状态</th><th>房东</th><th>房东电话</th><th>房间类型</th><th></th></tr>");
							}
							$("#tab").append(tr);
						});
					if(data.currentPage==1&&data.currentPage!=data.totalSize){
						$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");

						$("#mid").html(data.currentPage+"/"+data.totalSize);
						$("#next").html("<a href="+data.url+"?curPage="+data.nextPage+">下一页</a>");
						$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
						} if(data.currentPage==data.totalSize&&data.currentPage!=1){
							$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
							$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
							$("#prev").html("<a href="+data.url+"?curPage="+data.prevPage+">上一页</a>");
							$("#mid").html(data.currentPage+"/"+data.totalSize);
						}if(data.currentPage!=1&&data.currentPage!=data.totalSize){
						$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
						$("#prev").html("<a href="+data.url+"?curPage="+data.prevPage+">上一页</a>");
						$("#mid").html(data.currentPage+"/"+data.totalSize);
						$("#next").html("<a href="+data.url+"?curPage="+data.nextPage+">下一页</a>");
						$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
						}
						if(data.currentPage==1&&data.currentPage==data.totalSize){
							$("#first").html("<a href="+data.url+"?curPage=1>首页</a>");
							$("#mid").html(data.currentPage+"/"+data.totalSize);
							$("#last").html("<a href="+data.url+"?curPage="+data.totalSize+">末页</a>");
						}
					}
			});
			
		});
		//删除房间信息
		$("#delRoom").click(function(){
			var rid=$("#tab tr td input:checked").val();
			if(typeof(rid)=="undefined"){
				alert("请选择您要删除的房间信息");
				return;
			}
			$.ajax({
				url:"room/del",
				data:{"id":rid},
				type:"post",
				success:function(data){
					if(data==0){
						alert("删除失败");
					}else if(data==1){
						alert("当前房间有人居住，您不能删除");
					}else if(data==2){
						alert("删除成功");
						window.location.reload();
					}
				}
			});
		});
		
})
	
	</script>
</html>
